package com.kingdee.eas.custom.hr.compensation.app;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.JobFactory;
import com.kingdee.eas.basedata.org.JobInfo;
import com.kingdee.eas.basedata.org.PositionCollection;
import com.kingdee.eas.basedata.org.PositionFactory;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hr.compensation.UpgradePayEntryFactory;
import com.kingdee.eas.custom.hr.compensation.UpgradePayEntryInfo;
import com.kingdee.eas.hr.affair.AffairBizHistoryFactory;
import com.kingdee.eas.hr.affair.AffairBizHistoryInfo;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillEntryInfo;
import com.kingdee.eas.hr.affair.ReinviteFireBizBillEntryInfo;
import com.kingdee.eas.hr.affair.app.HRAffairBillBizBean;
import com.kingdee.eas.hr.base.HRBillBaseEntryInfo;
import com.kingdee.eas.hr.base.HRBillBaseInfo;
import com.kingdee.eas.hr.base.LoanOwnerEnum;
import com.kingdee.eas.hr.canon.cbase.AdminOrgBaseInfo;
import com.kingdee.eas.hr.canon.cbase.GradeInfo;
import com.kingdee.util.StringUtils;

public class UpgradePayBizBean extends HRAffairBillBizBean {

	@Override
	protected String getNewPositionField() {
		return null;
	}

	@Override
	protected String getOldPositionField() {
		return null;
	}

	@Override
	protected String getInnerState() {
		return "innerState";
	}

	@Override
	protected String getBillField() {
		return "parent";
	}

	@Override
	protected String getEntryEffectDateField() {
		return "effectDate";
	}

	@Override
	protected String getEntryField() {
		return "entrys";
	}

	@Override
	public void _beforeEffectOnNoJob(Context ctx, HRBillBaseEntryInfo entry)
			throws BOSException, EASBizException {
		UpgradePayEntryInfo entryInfo = (UpgradePayEntryInfo) entry;

		PositionInfo targetPosition = null;
		PositionInfo superPosition = null;

		AdminOrgUnitInfo targetAdminOrgUnit = entryInfo.getNewDept();
		targetAdminOrgUnit = AdminOrgUnitFactory.getLocalInstance(ctx)
				.getAdminOrgUnitInfo(
						new ObjectUuidPK(targetAdminOrgUnit.getId()));

		JobInfo targetJob = entryInfo.getNewJob();
		targetJob = JobFactory.getLocalInstance(ctx).getJobInfo(
				new ObjectUuidPK(targetJob.getId()));
		String targetPositionNum = targetAdminOrgUnit.getNumber()
				+ targetJob.getNumber();

		PositionCollection positionCollection = this.getPositionInfoByNumber(
				ctx, targetPositionNum);

		if (positionCollection.size() > 0) {
			targetPosition = positionCollection.get(0);
		} else {
			targetPosition = new PositionInfo();
			targetPosition.setNumber(targetPositionNum);
			targetPosition.setName(targetAdminOrgUnit.getName()
					+ targetJob.getName());
			targetPosition.setJob(targetJob);
			targetPosition.setAdminOrgUnit(targetAdminOrgUnit);
			targetPosition.setIsRespPosition(true);
			targetPosition.setIsView(true);
			targetPosition.setEffectDate(entryInfo.getEffectDate());
			targetPosition.setHrOrgUnit(entryInfo.getParent().getHrOrgUnit());

			positionCollection = this.getRespPositionByAdminOrgUnit(ctx,
					targetAdminOrgUnit.getId().toString());

			if (positionCollection.size() > 0) {
				superPosition = positionCollection.get(0);
			}
			if (superPosition != null) {
				targetPosition.put("SuperPosition", superPosition);
			} else {
				targetPosition.put("SuperPosition", "me");
			}

			PositionFactory.getLocalInstance(ctx).submit(targetPosition);
		}
		entryInfo.setNewPosition(targetPosition);
		UpgradePayEntryFactory.getLocalInstance(ctx).save(entryInfo);
		super._beforeEffectOnNoJob(ctx, entry);
	}

	private PositionCollection getPositionInfoByNumber(Context ctx,
			String number) throws EASBizException, BOSException {
		return PositionFactory.getLocalInstance(ctx).getPositionCollection(
				"where number='" + number + "'");
	}

	private PositionCollection getRespPositionByAdminOrgUnit(Context ctx,
			String targetAdminOrgUnit) throws BOSException {
		return PositionFactory.getLocalInstance(ctx).getPositionCollection(
				"where adminOrgUnit='" + targetAdminOrgUnit
						+ "' and isRespPosition=1");
	}

	@Override
	protected void saveAffairHistory(Context ctx, HRBillBaseEntryInfo entry)
			throws BOSException, EASBizException {

		UpgradePayEntryInfo entryInfo = (UpgradePayEntryInfo) entry;
		String billField = getBillField();
		String entryBizDate = getEntryEffectDateField();
		if (StringUtils.isEmpty(billField)) {
			return;
		}
		HRBillBaseInfo bill = (HRBillBaseInfo) entry.get(billField);
		GradeInfo oldGrade = entryInfo.getNowGrade();
		GradeInfo newGrade = entryInfo.getNewGrade();

		String oldId = null;
		String newId = null;

		if (oldGrade == null) {
			oldId = "";
		} else {
			oldId = oldGrade.getId().toString();
		}

		if (newGrade == null) {
			newId = "";
		} else {
			newId = newGrade.getId().toString();
		}

		if (oldId.equals(newId)) {
			return;
		}
		AdminOrgBaseInfo oldCostCenter = (AdminOrgBaseInfo) entryInfo
				.getNowAdminOrgBase();
		AdminOrgBaseInfo newCostCenter = (AdminOrgBaseInfo) entryInfo
				.getNewAdminOrgBase();
		if (bill != null) {
			AffairBizHistoryInfo historyInfo = new AffairBizHistoryInfo();

			historyInfo.setBillID(bill.getId());
			historyInfo.setBillBOSType(bill.getBOSType().toString());
			historyInfo.setBillAdmin(bill.getAdminOrg());
			historyInfo.setHrOrgUnit(bill.getHrOrgUnit());

			historyInfo.setEntryID(entry.getId());

			AdminOrgUnitInfo oldAdmin = null;
			AdminOrgUnitInfo newAdmin = null;
			PositionInfo oldPosition = null;
			PositionInfo newPosition = null;

			if (!StringUtils.isEmpty(entryBizDate)) {
				if ((entry instanceof EmpEnrollBizBillEntryInfo)) {
					historyInfo.setEffectDate(entry.getDate("enrollDate"));
				} else if ((entry instanceof ReinviteFireBizBillEntryInfo)) {
					historyInfo
							.setEffectDate(entry.getDate("reinviteFireDate"));
				} else {
					historyInfo.setEffectDate(entry.getDate(entryBizDate));
				}
			}

			if (!StringUtils.isEmpty(getPersonField())) {
				historyInfo.setPerson((PersonInfo) entry.get(getPersonField()));
			}
			if (!StringUtils.isEmpty(getOldAdminField())) {
				oldAdmin = (AdminOrgUnitInfo) entry.get(getOldAdminField());
				historyInfo.setOldAdmin(oldAdmin);
			}
			if (!StringUtils.isEmpty(getNewAdminField())) {
				newAdmin = (AdminOrgUnitInfo) entry.get(getNewAdminField());
				historyInfo.setNewAdmin(newAdmin);
			}
			if (!StringUtils.isEmpty(getOldPositionField())) {
				oldPosition = (PositionInfo) entry.get(getOldPositionField());
				historyInfo.setOldPosition(oldPosition);
			}
			if (!StringUtils.isEmpty(getNewPositionField())) {
				newPosition = (PositionInfo) entry.get(getNewPositionField());
				historyInfo.setNewPosition(newPosition);
			}

			historyInfo.setTuned(false);
			historyInfo.setOldGrade(oldGrade);
			historyInfo.setNewGrade(newGrade);
			historyInfo.setOldCostCenter(oldCostCenter);
			historyInfo.setNewCostCenter(newCostCenter);
			historyInfo.setOwner(LoanOwnerEnum.IN);
			setHistoryAdminInOut(historyInfo, oldAdmin, newAdmin, oldPosition,
					newPosition);

			AffairBizHistoryFactory.getLocalInstance(ctx).addnew(historyInfo);
		}
	}

	public void _check(Context ctx, IObjectPK pk, IObjectValue model)
			throws BOSException, EASBizException {
	}
}
